package OSMS.action.Boss;

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.entity.*;
import OSMS.dao.*;

public class GoodsUpdateServlet extends HttpServlet{

	private GoodsDao dao = new GoodsDao();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			

			request.setCharacterEncoding("utf-8");
			//从session中获取要更新的商品所属商家bid
			Object obj1 = request.getSession().getAttribute("gid");
			Object obj2 = request.getSession().getAttribute("bid");
			int gid = Integer.parseInt(obj1.toString());
			System.out.println(gid);
			int bid = Integer.parseInt(obj2.toString());
			
			//测试用
			//System.out.println(gid);
			//System.out.println(bid);
			
			
			//获取updategoods.jsp页面的参数 
			String gname = request.getParameter("gname");
			String gprice_str = request.getParameter("gprice");
			float gprice = Float.parseFloat(gprice_str);
			String gtype = request.getParameter("gtype");
			String picture = request.getParameter("picture");
			String gdetail = request.getParameter("gdetail");
			int gstore = Integer.parseInt(request.getParameter("gstore"));
			
			Goods goods =new Goods();
			goods.setBid(bid);
			goods.setGid(gid);
			goods.setGname(gname);
			goods.setGprice(gprice);
			goods.setGtype(gtype);
			goods.setPicture(picture);
			goods.setGdetail(gdetail);
			goods.setGstore(gstore);
			boolean flag = false;
			try{
				  //调用dao方法实现商品更新
				  flag = dao.updateGoods(goods);
				  //System.out.println(flag);
			}catch(Exception e) {
				e.printStackTrace();
			}
			System.out.println(flag);
			//跳转回商家旗下商品信息页面

			request.setCharacterEncoding("utf-8");
			request.getRequestDispatcher("../boss/bossindex").forward(request, response);
			
			
			
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	

}

